/*
 * @Author: kadu
 * @Date: 2021-06-26 22:08:59
 * @LastEditTime: 2021-06-26 22:34:15
 * @LastEditors: 时钟管理
 * @Description: In User Settings Edit
 */

export class TimerManager {
    private static _inst: TimerManager;
    static get inst() {
        if (this._inst) {
            return this._inst;
        }

        this._inst = new TimerManager();
        return this._inst;
    }

    protected updates: Array<any> = [];
    protected lateUpdates: Array<any> = [];

    public addUpdate(handler: any) {
        if (handler.update)
            this.updates.push(handler);
    }

    public addLateUpdate(handler: any) {
        if (handler.update)
            this.lateUpdates.push(handler);
    }

    public remove(handler: any) {
        for (let i = 0; i < this.updates.length; ++i) {
            if (this.updates[i] == handler) {
                this.updates.splice(i, 1);
                break;
            }
        }
        for (let i = 0; i < this.lateUpdates.length; ++i) {
            if (this.lateUpdates[i] == handler) {
                this.lateUpdates.splice(i, 1);
                break;
            }
        }
    }

    public update(deltaTime: number) {
        for (let i = 0; i < this.updates.length; ++i) {
            this.updates[i].update(deltaTime);
        }
    }

    public lateUpdate(deltaTime: number) {
        for (let i = 0; i < this.lateUpdates.length; ++i) {
            this.lateUpdates[i].lateUpdate(deltaTime);
        }
    }
}